﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Globalization;

namespace MySkiPatrolDAL
{
    /// <summary>
    /// Summary description for Converts
    /// </summary>
    public class DataConvertor
    {
        public enum DateInterval
        {
            Day,
            DayOfYear,
            Hour,
            Minute,
            Month,
            Quarter,
            Second,
            Weekday,
            WeekOfYear,
            Year
        }

        public DataConvertor()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //private static void writeLog(string message)
        //{
        //    DateTime dt = DateTime.Now;
        //    string dttm = dt.ToShortDateString() + " - " + dt.ToShortTimeString();

        //    StreamWriter strmWrt = new StreamWriter(System.Web.HttpContext.Current.Application["LogPath"] + "CONVERTS.LOG", true);
        //    strmWrt.WriteLine("[" + dttm + "] " + message + "\r");
        //    strmWrt.Close();

        //}

        //private static void writeError(Exception ex, string colName)
        //{
        //    writeLog("Error reading column " + colName + ".  " + ex.Message );
        //}

        public static DateTime MinDate = DateTime.Parse("1-1-1900");

        public static bool toBool(DataRow myRow, string myColumn)
        {
            bool myReturn = false;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = false;
                }
                else
                {
                    switch (myRow[myColumn].ToString().TrimEnd().ToUpper())
                    {
                        //FALSE values
                        case "N":
                        case "NO":
                        case "0":
                        case "FALSE":
                            myReturn = false;
                            break;
                        //TRUE values
                        case "Y":
                        case "YES":
                        case "1":
                        case "-1":
                        case "TRUE":
                            myReturn = true;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static bool toBool(string data)
        {
            bool myReturn = false;
            try
            {
                if (string.IsNullOrEmpty(data))
                {
                    myReturn = false;
                }
                else
                {
                    switch (data.ToUpper())
                    {
                        //FALSE values
                        case "N":
                        case "NO":
                        case "0":
                        case "FALSE":
                            myReturn = false;
                            break;
                        //TRUE values
                        case "Y":
                        case "YES":
                        case "1":
                        case "-1":
                        case "TRUE":
                            myReturn = true;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static double toDouble(DataRow myRow, string myColumn)
        {
            double myReturn = 0;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = 0;
                }
                else
                {
                    myReturn = double.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static decimal toDecimal(DataRow myRow, string myColumn)
        {
            decimal myReturn = 0;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = 0;
                }
                else
                {
                    myReturn = decimal.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static float toFloat(DataRow myRow, string myColumn)
        {
            float myReturn = 0;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = 0;
                }
                else
                {
                    myReturn = float.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static int toInt(DataRow myRow, string myColumn)
        {
            int myReturn = 0;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = 0;
                }
                else
                {
                    myReturn = int.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static string toTitleCase(DataRow myRow, string myColumn)
        {
            string myReturn = "";
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = "";
                }
                else
                {
                    myReturn = myRow[myColumn].ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }

            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            myReturn = textInfo.ToTitleCase(myReturn.ToLower()); 

            return myReturn;
        }



        public static string toString(DataRow myRow, string myColumn)
        {
            string myReturn = "";
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = "";
                }
                else
                {
                    myReturn = myRow[myColumn].ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static DateTime toDateTime(DataRow myRow, string myColumn)
        {
            DateTime myReturn = DataConvertor.MinDate;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = DataConvertor.MinDate;
                }
                else
                {
                    myReturn = DateTime.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static DateTimeOffset toDateTimeOffset(DataRow myRow, string myColumn)
        {
            DateTimeOffset myReturn = DateTimeOffset.MinValue;
            try
            {
                if (myRow.IsNull(myColumn))
                {
                    myReturn = DateTimeOffset.MinValue;
                }
                else
                {
                    myReturn = DateTimeOffset.Parse(myRow[myColumn].ToString().Trim());
                }
            }
            catch (Exception ex)
            {
                //writeError(ex, myColumn);
                throw ex;
            }
            return myReturn;
        }

        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2)
        {
            return DateDiff(interval, dt1, dt2, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
        }

        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2, DayOfWeek eFirstDayOfWeek)
        {
            if (interval == DateInterval.Year)
                return dt2.Year - dt1.Year;

            if (interval == DateInterval.Month)
                return (dt2.Month - dt1.Month) + (12 * (dt2.Year - dt1.Year));

            TimeSpan ts = dt2 - dt1;

            if (interval == DateInterval.Day || interval == DateInterval.DayOfYear)
                return Round(ts.TotalDays);

            if (interval == DateInterval.Hour)
                return Round(ts.TotalHours);

            if (interval == DateInterval.Minute)
                return Round(ts.TotalMinutes);

            if (interval == DateInterval.Second)
                return Round(ts.TotalSeconds);

            if (interval == DateInterval.Weekday)
            {
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.WeekOfYear)
            {
                while (dt2.DayOfWeek != eFirstDayOfWeek)
                    dt2 = dt2.AddDays(-1);
                while (dt1.DayOfWeek != eFirstDayOfWeek)
                    dt1 = dt1.AddDays(-1);
                ts = dt2 - dt1;
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.Quarter)
            {
                double d1Quarter = GetQuarter(dt1.Month);
                double d2Quarter = GetQuarter(dt2.Month);
                double d1 = d2Quarter - d1Quarter;
                double d2 = (4 * (dt2.Year - dt1.Year));
                return Round(d1 + d2);
            }

            return 0;

        }

        private static long Round(double dVal)
        {
            if (dVal >= 0)
                return (long)Math.Floor(dVal);
            return (long)Math.Ceiling(dVal);
        }

        private static int GetQuarter(int nMonth)
        {
            if (nMonth <= 3)
                return 1;
            if (nMonth <= 6)
                return 2;
            if (nMonth <= 9)
                return 3;
            return 4;
        }

        private string base64Encode(string sData)
        {
            try
            {
                byte[] encData_byte = new byte[sData.Length];
                encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);
                string encodedData = Convert.ToBase64String(encData_byte);
                return encodedData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in base64Encode" + ex.Message);
            }
        }

        public string base64Decode(string sData)
        {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(sData);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
        }

        public static string ShortText(string data, int characters)
        {
            string myReturn = string.Empty;

            if (data.Length > characters)
            {
                //get the # of characters
                myReturn = data.Substring(0, characters);

                //now back up the characters to the last word...
                myReturn = myReturn.Substring(0, myReturn.LastIndexOf(" "));

                //add the "..." at the end
                myReturn = myReturn + "...";
            }
            else
            {
                myReturn = data;
            }
            return myReturn;
        }
    }
}